package com.ruoyi.project.kkad.adplace.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.ruoyi.common.constant.RedisKey;
import com.ruoyi.common.utils.CodeUtil;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.kkad.adplace.domain.AdvertisingPlace;
import com.ruoyi.project.kkad.adplace.mapper.AdvertisingPlaceMapper;
import com.ruoyi.project.kkad.adplace.service.IAdvertisingPlaceService;

/**
 * 广告位管理Service业务层处理
 * 
 * @author shixiangwen
 * @date 2020-04-13
 */
@Service
@DataSource(DataSourceType.KKAD)
public class AdvertisingPlaceServiceImpl implements IAdvertisingPlaceService {

	@Autowired
	@Qualifier("commonRedisTemplate")
	RedisTemplate<String, String> redisTemplate;

	@Autowired
	private AdvertisingPlaceMapper advertisingPlaceMapper;

	/**
	 * 查询广告位管理
	 * 
	 * @param id 广告位管理ID
	 * @return 广告位管理
	 */
	@Override
	public AdvertisingPlace selectAdvertisingPlaceById(Long id) {
		return advertisingPlaceMapper.selectAdvertisingPlaceById(id);
	}

	/**
	 * 查询广告位管理列表
	 * 
	 * @param advertisingPlace 广告位管理
	 * @return 广告位管理
	 */
	@Override
	public List<AdvertisingPlace> selectAdvertisingPlaceList(AdvertisingPlace advertisingPlace) {
		List<AdvertisingPlace> list = advertisingPlaceMapper.selectAdvertisingPlaceList(advertisingPlace);
		list.forEach(item -> {
			item.setName(item.getRemark());
		});
		return list;
	}

	/**
	 * 新增广告位管理
	 * 
	 * @param advertisingPlace 广告位管理
	 * @return 结果
	 */
	@Override
	public int insertAdvertisingPlace(AdvertisingPlace advertisingPlace) {
		advertisingPlace.setCode(CodeUtil.get(10));
		return advertisingPlaceMapper.insertAdvertisingPlace(advertisingPlace);
	}

	/**
	 * 修改广告位管理
	 * 
	 * @param advertisingPlace 广告位管理
	 * @return 结果
	 */
	@Override
	public int updateAdvertisingPlace(AdvertisingPlace advertisingPlace) {
		AdvertisingPlace old = advertisingPlaceMapper.selectAdvertisingPlaceById(advertisingPlace.getId());
		redisTemplate.opsForHash().delete(RedisKey.Ad.getAdvertisingPlaceKey(), old.getCode());
		return advertisingPlaceMapper.updateAdvertisingPlace(advertisingPlace);
	}

	/**
	 * 删除广告位管理对象
	 * 
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteAdvertisingPlaceByIds(String ids) {
		return advertisingPlaceMapper.deleteAdvertisingPlaceByIds(Convert.toStrArray(ids));
	}

	/**
	 * 删除广告位管理信息
	 * 
	 * @param id 广告位管理ID
	 * @return 结果
	 */
	@Override
	public int deleteAdvertisingPlaceById(Long id) {
		return advertisingPlaceMapper.deleteAdvertisingPlaceById(id);
	}
}
